﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Atomic.User.Entities;
using Atomic.Db;
using System.Data;
using NHibernate;

namespace Atomic.User.DAL
{
    public class UserTypeDAL : BaseDAL<UserTypeEntity>
    {
        public UserTypeDAL()
            : base() { }
        public UserTypeDAL(ISession session)
            : base(session) { }

        /// <summary>
        /// 覆盖方法，创建新的用户类型
        /// </summary>
        /// <param name="entity">用户类型信息</param>
        /// <returns>是否创建成功</returns>
        public new object Add(UserTypeEntity entity)
        {
            return session.Save(entity);   //保存用户类型
        }

        /// <summary>
        /// 删除类型记录
        /// </summary>
        /// <param name="id"></param>
        public override void Remove(object id)
        {
            UserTypeEntity entity = session.Get<UserTypeEntity>(id);
            session.Delete(entity); //删除类型记录
        }

        /// <summary>
        /// 创建用户类型对应的数据表
        /// </summary>
        /// <param name="id">用户类型ID</param>
        public virtual void CreateUserExtendTable(object id)
        {
            session.CreateSQLQuery(string.Format("CREATE TABLE [dbo].[UserExtend{0}]([ID] [int] NOT NULL CONSTRAINT [{1}] PRIMARY KEY CLUSTERED ([ID] ASC)) ON [PRIMARY]",
                id,
                "PK_UserExtend" + id)).ExecuteUpdate();  //创建新的用户类型对于的扩展属性表
        }

        /// <summary>
        /// 删除用户类型对应的数据表
        /// </summary>
        /// <param name="id">用户类型ID</param>
        public virtual void DropUserExtendTable(object id)
        {
            session.CreateSQLQuery(string.Format("Drop TABLE [dbo].[UserExtend{0}]",
                id)).ExecuteUpdate();  //创建新的用户类型对于的扩展属性表
        }
    }
}
